<template>
  <div>
    <div class="head"><strong>首页</strong> > 商品管理 > 商品分类</div>
    <el-card>
      <el-button type="primary" @click="jok">添加分类</el-button>
      <tree-table
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        style="margin-top: 10px"
      >
        <template slot="goo" slot-scope="voh">
          <i
            class="el-icon-success"
            v-if="voh.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: lightred"></i>
        </template>
        <template slot="gop" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="gos" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="log(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="remove(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querryinfo.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="querryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addclose"
    >
      <el-form
        :model="addForm"
        :rules="addform"
        ref="addFromRef"
        label-width="90px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="gosy"
            :options="paralist"
            :props="sol"
            @change="handleChange"
            style="width: 100%"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="cate">确 定</el-button>
      </span>
    </el-dialog>
     <el-dialog
  title="修改分类"
  :visible.sync="bott"
  width="50%"
  >
  <el-form
        :model="addForm"
        :rules="addform"
        ref="addFromRef"
        label-width="90px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        
      </el-form>
  
  <span slot="footer" class="dialog-footer">
    <el-button @click="bott = false">取 消</el-button>
    <el-button type="primary" @click="spo">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getcatelist()
  },
  data() {
    return {
        bott:false,
      gosy: [], //选中的分类必须为数组
      sol: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        checkStrictly: 'true',
      },
      addForm: {
        //父级分类的id
        cat_pid: 0,
        cat_name: '',
        //默认要添加的分类是一级分类
        cat_level: 0,
      },
      addform: {
        cat_name: [
          {
            required: true,
            messgae: '请输入分类名称',
            trigger: 'blur',
          },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },

      dialogVisible: false,
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        {
          label: '状态',
          //表示定义当前列为template
          type: 'template',
          //在一列使用的模板名称
          template: 'goo',
        },
        {
          label: '排序',
          type: 'template',
          template: 'gop',
        },
        {
          label: '操作',
          type: 'template',
          template: 'gos',
        },
      ],
      total: 0,
      catelist: [],
      //父级分类
      paralist: [],
      //查询条件
      querryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
    }
  },
  methods: {
    handleSizeChange(ms) {
      this.querryinfo.pagesize = ms
      this.getcatelist()
    },
    handleCurrentChange(mk) {
      this.querryinfo.pagenum = mk
      this.getcatelist()
    },
    async getcatelist() {
      const { data: res } = await this.$http.get('categories', {
        params: this.querryinfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      console.log(res)
      this.catelist = res.data.result
      this.total = res.data.total
    },
    jok() {
      this.getparalist()
      this.dialogVisible = true
    },
    async getparalist() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      //  console.log(res.data);
      this.paralist = res.data
    },
    handleChange() {
      //console.log(this.gosy);
      //判断gosy种是否有数组
      if (this.gosy.length > 0) {
        this.addForm.cat_pid = this.gosy[this.gosy.length - 1]
        this.addForm.cat_level = this.gosy.length
      } else {
        this.addForm.cat_pid = 0
        this.addForm.cat_level = 0
      }
    },
    cate() {
      this.$refs.addFromRef.validate(async (valid) => {
       
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addForm)
        if (res.meta.status !== 201) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        this.getcatelist()
        this.dialogVisible = false
      })

      //console.log(this.addForm);
    },
    addclose() {
      this.$refs.addFromRef.resetFields()
      ;(this.gosy = []), (this.addForm.cat_pid = 0)
      this.addForm.cat_level = 0
    },
     adclose(){
        this.$refs.addFromRef.resetFields()}
    ,
     log(dk){
          console.log(dk);
          this.addForm=dk
          console.log(this.addForm.cat_id);
         this.bott=true
     },
    spo(){
     this.$refs.addFromRef.validate(async valid=>{
            if(!valid)return
           const {data:res}=await this.$http.put('categories/'+this.addForm.cat_id,this.addForm)
           console.log(res);
          if(res.meta.status!==200){
              return this.$message.error(res.meta.msg)
          }
          this.$message.success(res.meta.msg)
          this.getcatelist()
          this.bott=false
       })
     },
      async remove(id) {
      const bug = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      //console.log(bug)
      //成功时是confirm
      //取消是cancel
      if (bug !== 'confirm') {
        this.$message.info('已取消删除')
      } else {
        const { data: res } = await this.$http.delete(`categories/${id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除用户失败')
        }
        this.$message.success('删除成功')
        this.getcatelist()
      }
    },

  },
}
</script>

<style lang='less' scoped>
.head {
  margin-left: 20px;
  margin-top: 25px;
  font-size: 14px;
  margin-bottom: 20px;
}
</style>